package com.ncloud.product.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.ncloud.product.entity.Discnt;

@Mapper
public interface DiscntMapper {

	@Select("SELECT a.discnt_id,a.discnt_name,a.discnt_explain,a.prod_id,a.billing_cycle,a.cycle_unit,a.discnt_type,"
			+ " a.discnt_state,a.create_date,a.create_user,a.update_date,a.update_user,a.remark"
			+ " FROM bs_p_discnt a, bs_p_discnt_release b"
			+ " WHERE a.discnt_id = b.discnt_id AND a.discnt_state = '1' and b.chnl_id = #{chnlId} "
			+ " AND b.prod_id = #{prodId} AND NOW() BETWEEN b.eff_date AND b.exp_date order by b.order_no")
	@Results({ @Result(column = "discnt_id", property = "discntId"),@Result(column="discnt_explain",property="discntExplain"),
			@Result(column = "discnt_name", property = "discntName"), @Result(column = "prod_id", property = "prodId"),
			@Result(column = "billing_cycle", property = "billingCycle"),
			@Result(column = "cycle_unit", property = "cycleUnit"),
			@Result(column = "discnt_type", property = "discntType"),
			@Result(column = "discnt_state", property = "discntState"),
			@Result(column = "create_date", property = "createDate"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_date", property = "updateDate"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	List<Discnt> getDiscntListByProd(@Param("prodId")Long prodId,@Param("chnlId")Long chnlId);

	@Select("SELECT a.* FROM bs_p_discnt a WHERE a.discnt_id = #{discntId} AND a.discnt_state = '1'")
	@Results({ @Result(column = "discnt_id", property = "discntId"),@Result(column="discnt_explain",property="discntExplain"),
			@Result(column = "discnt_name", property = "discntName"), @Result(column = "prod_id", property = "prodId"),
			@Result(column = "billing_cycle", property = "billingCycle"),
			@Result(column = "cycle_unit", property = "cycleUnit"),
			@Result(column = "discnt_type", property = "discntType"),
			@Result(column = "discnt_state", property = "discntState"),
			@Result(column = "create_date", property = "createDate"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_date", property = "updateDate"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	Discnt getDiscntById(Long discntId);
	
	@Select("SELECT a.* FROM bs_p_discnt a WHERE a.discnt_type='0' and a.prod_id = #{prodId} AND a.discnt_state = '1'")
	@Results({ @Result(column = "discnt_id", property = "discntId"),@Result(column="discnt_explain",property="discntExplain"),
			@Result(column = "discnt_name", property = "discntName"), @Result(column = "prod_id", property = "prodId"),
			@Result(column = "billing_cycle", property = "billingCycle"),
			@Result(column = "cycle_unit", property = "cycleUnit"),
			@Result(column = "discnt_type", property = "discntType"),
			@Result(column = "discnt_state", property = "discntState"),
			@Result(column = "create_date", property = "createDate"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_date", property = "updateDate"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	Discnt getBaseDiscntByProdId(Long prodId);
	
	@Select("SELECT a.* FROM bs_p_discnt a WHERE a.discnt_id = #{discntId} AND a.discnt_state = '1'"
			+ " and exists(select 1 from bs_p_discnt_release b where a.discnt_id = b.discnt_id and b.chnl_id = #{chnlId}"
			+ " and now() between b.eff_date and b.exp_date)")
	@Results({ @Result(column = "discnt_id", property = "discntId"),@Result(column="discnt_explain",property="discntExplain"),
			@Result(column = "discnt_name", property = "discntName"), @Result(column = "prod_id", property = "prodId"),
			@Result(column = "billing_cycle", property = "billingCycle"),
			@Result(column = "cycle_unit", property = "cycleUnit"),
			@Result(column = "discnt_type", property = "discntType"),
			@Result(column = "discnt_state", property = "discntState"),
			@Result(column = "create_date", property = "createDate"),
			@Result(column = "create_user", property = "createUser"),
			@Result(column = "update_date", property = "updateDate"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark") })
	Discnt getChnlDiscntById(@Param("disnctId")Long discntId,@Param("chnlId")Long chnlId);

}
